package com.gjs.cd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.cd.pojo.dto.YearSubjectCodeReq;
import com.gjs.cd.pojo.entity.CdYearSubjectCode;
import com.gjs.cd.pojo.vo.YearSubjectCodeVo;
import com.gjs.cd.service.ICdCodeValidTermService;
import com.gjs.cd.service.ICdYearSubjectCodeService;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.microservice.api.IResourceMicroService;
import com.gjs.common.pojo.dto.request.PageQuery;
import com.gjs.common.pojo.dto.response.ListPageRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhongwenguang
 * @date 2022-3-11 12:00
 */
@RestController
@Slf4j
@Api(tags = "光盘年度学科代码接口")
@RequestMapping("/ysc")
@RequiredArgsConstructor
public class CdYearSubjectCodeController {

    private final ICdYearSubjectCodeService iYearSubjectCodeService;
    private final ICdCodeValidTermService iCdCodeValidTermService;
    @DubboReference
    private IResourceMicroService iResourceMicroService;

    @PostMapping("/add")
    @ApiOperation("设置年度学科代码")
    public RspBody<Boolean> add(@RequestBody YearSubjectCodeReq yearSubjectCodeReq){
        CdYearSubjectCode yearSubjectCode = BeanUtil.copyProperties(yearSubjectCodeReq, CdYearSubjectCode.class);
        String code = yearSubjectCode.getSubjectCode().toLowerCase();//获取代码并转小写
        if (code.length()>1){
            return RspBody.error("学科代码只能是a-y中的一个字母！");
        }
        Pattern pattern = Pattern.compile("[a-y]*");
        Matcher matcher = pattern.matcher(code);
        if (!matcher.matches()){
            return RspBody.error("学科代码只能是a-y中的一个字母！");
        }
        yearSubjectCode.setSubjectCode(code);
        Boolean b = iYearSubjectCodeService.save(yearSubjectCode);
        return RspBody.success(b);
    }
    @GetMapping("/list")
    @ApiOperation("查询分页")
    public RspBody<ListPageRsp<YearSubjectCodeVo>> list(PageQuery pageQuery,YearSubjectCodeReq yearSubjectCodeReq){
        IPage<YearSubjectCodeVo> iPage = iYearSubjectCodeService.getList(pageQuery,yearSubjectCodeReq);
        return RspBody.success(ListPageRsp.of(iPage,YearSubjectCodeVo.class));
    }

    @GetMapping("get_code_list")
    @ApiOperation("获取已使用的学科代码")
    public RspBody<List<CdYearSubjectCode>> getCodeList(){
        return RspBody.success(iYearSubjectCodeService.getCodeList());
    }

}
